20180410
標準python找尋string的方法如下 (回答True/False)
word1='bird'
word2='dog'
string='bird on tree'
print (word1 in string)
print (word2 in string)
#Output#
#True
#False
使用regular expression方法的話就會變成
import re #導入模組
word1='bird'
word2='dog'
string='bird & tree'
print (re.search(word1,string))
print (re.search(word2,string))
#Output#
#<_sre.SRE_Match object; span=(0, 4), match='bird'>
#None
a)給定一表達式的方法為在字串前面加上r。
b)字串中使用[] (中括號) 來做模糊搜尋,括號中的字元都會匹配。
c)字串中使用\d 表示用所有數字去匹配、\D表示用所有非數字去匹配。
......etc (可到網路上查所有使用方式&頁尾有附圖)
print (re.search(r"bir\D", "bird & tree"))
#Output#
#<_sre.SRE_Match object; span=(0, 4), match='bird'>
使用compile 給定一個group之後可以只取出match的部分 ((*原因待學習....
word3=re.compile(r'bir\D')
match = re.search(word3, "bird & tree")
print (match)
print (match.group())
#Output#
#<_sre.SRE_Match object; span=(0, 4), match='bird'>
#bird